package com.cookman.assets;

import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.cookman.world.entities.City;

import java.util.HashMap;


/**
 * Created by Cookman on 04.04.14.
 */
public class AssetsImpl implements Assets {
    public static final AssetsImpl instance = new AssetsImpl();

    public static final String MAP = "map";
    public static final String CITY = "city";
    public static final String CITY_CARD = "cityCard";
    public static final String ACTIONS = "actions";

    private AssetManager assetManager;

    private HashMap<String, AssetStorage> storageMap = new HashMap<String, AssetStorage>();

    public AssetStorage map;
    public AssetCity city;
    public AssetCityCard cityCard;
    public AssetActions actions;


    @Override
    public Sprite getSpriteByAtlasRegion(String group, String key) throws Exception {
        if (storageMap.containsKey(group)) {
            AssetStorage storage = storageMap.get(group);
            return new Sprite(storage.getRegion(key));
        }else {
            throw new Exception("Assets storage not found");
        }
    }

    private AssetsImpl() {
    }

    public static TextureAtlas atlas;

    public void init(AssetManager assetManager) {
        this.assetManager = assetManager;
        assetManager.load("images/alice.atlas", TextureAtlas.class);
        assetManager.finishLoading();
        atlas = assetManager.get("images/alice.atlas");
        storageMap.put(MAP, new AssetMap(atlas));
        storageMap.put(CITY, new AssetCity(atlas));
        storageMap.put(CITY_CARD, new AssetCityCard(atlas));
        storageMap.put(ACTIONS, new AssetActions(atlas));
    }

    @Override
    public void error(AssetDescriptor asset, Throwable throwable) {

    }

    @Override
    public void dispose() {
        assetManager.dispose();
    }


}

